package com.jsecode.lib.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class LittleEndian {

    private static Logger logger = LoggerFactory.getLogger("LittleEndian");
    
    public static void setLong4(byte[] array, int index, long value) {
        array[index]     = (byte) value;
        array[index + 1] = (byte) (value >>> 8);
        array[index + 2] = (byte) (value >>> 16);
        array[index + 3] = (byte) (value >>> 24);
    }

    public static long getLong4(byte[] array, int index) {
        return (long) array[index] & 0xff |
               ((long) array[index + 1] & 0xff) <<  8 |
               ((long) array[index + 2] & 0xff) << 16 |
               ((long) array[index + 3] & 0xff) << 24;
    }
    

    public static long getLong6(byte[] array, int index) {
        return (long) array[index] & 0xff |
                ((long) array[index + 1] & 0xff) <<  8 |
                ((long) array[index + 2] & 0xff) << 16 |
                ((long) array[index + 3] & 0xff) << 24 |
                ((long) array[index + 4] & 0xff) << 32 |
                ((long) array[index + 5] & 0xff) << 40;
    }

    public static void setLong(byte[] array, int index, long  value) {
        array[index]     = (byte) value;
        array[index + 1] = (byte) (value >>> 8);
        array[index + 2] = (byte) (value >>> 16);
        array[index + 3] = (byte) (value >>> 24);
        array[index + 4] = (byte) (value >>> 32);
        array[index + 5] = (byte) (value >>> 40);
        array[index + 6] = (byte) (value >>> 48);
        array[index + 7] = (byte) (value >>> 56);
    }

    public static long getLong(byte[] array, int index) {
        return (long) array[index] & 0xff |
               ((long) array[index + 1] & 0xff) <<  8 |
               ((long) array[index + 2] & 0xff) << 16 |
               ((long) array[index + 3] & 0xff) << 24 |
               ((long) array[index + 4] & 0xff) << 32 |
               ((long) array[index + 5] & 0xff) << 40 |
               ((long) array[index + 6] & 0xff) << 48 |
               ((long) array[index + 7] & 0xff) << 56;
    }

    public static void setInt2(byte[] array, int index, int value) {
        array[index]     = (byte) value;
        array[index + 1] = (byte) (value >>> 8);
        array[index + 2] = (byte) (value >>> 16);
    }
 
    public static int getInt2(byte[] array, int index) {
        return array[index] & 0xff |
               (array[index + 1] & 0xff) <<  8 |
               (array[index + 2] & 0xff) << 16;
    }

    public static void setInt(byte[] array, int index, int value) {
        array[index]     = (byte) value;
        array[index + 1] = (byte) (value >>> 8);
        array[index + 2] = (byte) (value >>> 16);
        array[index + 3] = (byte) (value >>> 24);
    }
   
    public static int getInt(byte[] array, int index) {
        return array[index] & 0xff |
               (array[index + 1] & 0xff) <<  8 |
               (array[index + 2] & 0xff) << 16 |
               (array[index + 3] & 0xff) << 24;
    }

    public static void setShort(byte[] array, int index, int value) {
        array[index]     = (byte) value;
        array[index + 1] = (byte) (value >>> 8);
    }
    
    public static short getShort(byte[] array, int index) {
        return (short) (array[index] & 0xFF | array[index + 1] << 8);
    }
}
